package edu.nf.project.mappers.storeOperation.convention;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.nf.project.entitys.dto.storeOperation.RefundDTO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @author CSY
 * @date 2025 - 06 - 20
 */
public interface RefundDTOMapper extends BaseMapper<RefundDTO> {
    // RefundDTOMapper接口修改
    @Results(id = "RefundDTOResultMap", value = {
            @Result(property = "rrId", column = "rrId", id = true),
            @Result(property = "orderId", column = "orderId"),
            @Result(property = "reason", column = "reason"),
            @Result(property = "refundDesc", column = "refundDesc"),
            @Result(property = "requestTime", column = "requestTime"),
            @Result(property = "requestAmount", column = "requestAmount"),
            @Result(property = "actualAmount", column = "actualAmount"),
            @Result(property = "requestStatus", column = "requestStatus"),
            @Result(property = "refuseReason", column = "refuseReason"),
            @Result(property = "operateTime", column = "operateTime"),
            @Result(property = "uId", column = "uId"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "customerFullName", column = "customerFullName"),

            // 一对多关系映射，自动聚合imagePath
            @Result(
                    property = "imagePath",
                    column = "rrId",
                    many = @Many(select = "edu.nf.project.mappers.storeOperation.convention.RefundImageMapper.selectImagePathsByRrId")
            )
    })
    @Select("SELECT rr.rr_id rrId,rr.order_id orderId,reason,\n" +
            "refund_desc refundDesc,request_time requestTime,\n" +
            "request_amount requestAmount,actual_amount actualAmount,\n" +
            "request_status requestStatus,refuse_reason refuseReason,\n" +
            "operate_time operateTime,rr.u_id uId,pay_way payWay,\n" +
            "phone,customer_fullname customerFullName\n" +
            "FROM refund_request rr \n" +
            "JOIN users u ON rr.u_id = u.u_id \n" +
            "JOIN customer_info ci ON u.u_id = ci.u_id \n" +
            "join pay_history ph on rr.order_id = ph.order_id "+
            "WHERE rr.rr_id = #{rrId} \n")
    RefundDTO selectRefundDTOByRrId(@Param("rrId") Integer rrId);

    @Select("select rr.rr_id rrId,order_id orderId,reason,\n" +
            "refund_desc refundDesc,request_time requestTime,\n" +
            "request_amount requestAmount,actual_amount actualAmount,\n" +
            "request_status requestStatus,refuse_reason refuseReason,\n" +
            "operate_time operateTime,rr.u_id uId,\n" +
            "phone,customer_fullname customerFullName\n" +
            "from refund_request rr join users u on rr.u_id = u.u_id\n" +
            "join customer_info ci on u.u_id = ci.u_id\n" +
            "where request_status <> 3")
    List<RefundDTO> listRefundDTO();
}
